import machine
import utime
import _thread
import framebuf
 
sda=machine.Pin(6)
scl=machine.Pin(7)
 
i2c=machine.I2C(1, sda=sda, scl=scl, freq=50000)

print(i2c.scan())
 
from ssd1306 import SSD1306_I2C

oled = SSD1306_I2C(128, 32, i2c)
button1 = machine.Pin(0,machine.Pin.OUT)
button2 = machine.Pin(1,machine.Pin.OUT)
button3 = machine.Pin(2,machine.Pin.OUT)
button4 = machine.Pin(3,machine.Pin.OUT)
button5 = machine.Pin(4,machine.Pin.OUT)
button6 = machine.Pin(5,machine.Pin.OUT)
 
led1 = machine.Pin(16,machine.Pin.OUT)
led2 = machine.Pin(17,machine.Pin.OUT)
ledStatus1 = machine.Pin(15,machine.Pin.OUT)
ledStatus2 = machine.Pin(25,machine.Pin.OUT)

ledStatus1.value(1)
ledStatus2.value(0)
led1.value(1)
led2.value(1)
 
print(i2c.scan())

led = 1
key = 0
lid = 1

classname=['Data Structure','Comp Compersition','Android','Physics','American History','PE','E-Engineering','Digital Logic','Policy','E-EngineerEX',
           'Data Structure','Comp principle','Android','Physics','American History','PE','E-Engineering','Digital Logic','Policy','Data Structure']

time=['22 8:30-10:05','21 8:30-10:05','20 18:30-20:55','20 8:30-10:05','19 18:30-20:55','19 14:40-16:15','19 10:25-12:00','19 8:30-10:05','18 10:25-12:00','18 14:40-16:15',
      '15 8:30-10:05','14 8:30-10:05','13 18:30-20:55','13 8:30-10:05','12 18:30-20:55','12 14:40-16:15','12 10:25-12:00','12 8:30-10:05','11 14:40-16:15','11 10:25-12:00']

place=['J2-220','J2-424','J2-324','J1-402','J3-102','Swimming Pool','J1-325','J2-420','J3-107','S3-401',
      'J2-220','J2-424','J2-324','J1-402','J3-102','Swimming Pool','J1-325','J2-420','J3-107','J4-212',]

c = classname.pop()
t = time.pop()
p = place.pop()

classnamed=[]
timed=[]
placed=[]

buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)

oled.text('Welcome using', 0, 0)
oled.text('Half Life', 0, 10)
oled.text('Russel Gloves', 0, 20)
oled.show()
utime.sleep(3)
oled.fill(1)
oled.show()
utime.sleep(3)
oled.fill(0)
oled.show()
utime.sleep(1)

def BackGround1(ledStatus1,ledStatus2):
    while True:
        ledStatus1.toggle()
        ledStatus2.toggle()
        utime.sleep(0.1)
        ledStatus1.toggle()
        ledStatus2.toggle()
        utime.sleep(3)


try:
    _thread.start_new_thread(BackGround1,(ledStatus1,ledStatus2))
except:
    print("Error: unable to start thread")

while True:
        if led==1:
            if lid==1:
                if key==1:
                    oled.fill(0)
                    oled.show()
                    oled.text(c,0,0)
                    oled.text(t,0,10)
                    oled.text(p,0,20)
                    oled.show()
                    utime.sleep(3)
                else:
                    oled.fill(0)
                    oled.show()
                    oled.blit(fb, 0, 0)
                    oled.text("Half Life",40,10)
                    oled.show()
                    utime.sleep(0.6)
                    led = 0
            elif lid==0:
                oled.fill(0)
                oled.show()
                utime.sleep(1)
                led = 0
                
        if button2.value()==1:
            if lid==0:
                lid=1
                led=1
            else:
                lid=0
                led=1
                
        if button1.value()==1:
            key=1
            led=1
        else:
            key=0
            
        if button3.value()==1:
            if len(classname)==0:
                now = 'class is out!'
                utime.sleep(0.6)
            else:
                classnamed.append(c)
                timed.append(t)
                placed.append(p)
                c = classname.pop()
                t = time.pop()
                p = place.pop()
                oled.fill(0)
                oled.show()
                oled.text("class set!",30,10)
                oled.show()
                utime.sleep(3)
                led=1
                
        if button5.value()==1:
            if len(classnamed)==0:
                now = 'front class out'
                utime.sleep(0.6)
            else:
                classname.append(c)
                time.append(t)
                place.append(p)
                c = classnamed.pop()
                t = timed.pop()
                p = placed.pop()
                oled.fill(0)
                oled.show()
                oled.text("front class set",0,10)
                oled.show()
                utime.sleep(3)
                led=1
            
        if button6.value()==1:
            oled.fill(1)
            oled.show()
            utime.sleep(5)
            led=1
            
        if button4.value()==1:
            led1.toggle()
            led2.toggle()
            utime.sleep(0.5)
